/* 
 * Copyright 2011-2012 Andrew Brock
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.catchwa.myvideobrowser;

import org.catchwa.myvideobrowser.model.Movie;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Andrew Brock
 */
public class MovieScanner {

  public static Result scan(File root, List<Movie> existing) {
    ArrayList<File> movies = new ArrayList<File>();
    File[] files = root.listFiles();
    for (File f : files) {
      if (f.isDirectory()) {
        movies.add(f);
      } else if (Main.isVideoFile(f)) {
        movies.add(f);
      }
    }
    if (existing != null) {
      // Remove movies that have already been scanned
      for (int i = 0; i < movies.size(); i++) {
        for (int j = 0; j < existing.size(); j++) {
          if (existing.get(j).getFile().getAbsolutePath().equals(movies.get(i).getAbsolutePath())) {
            movies.remove(i);
            existing.remove(j);
            j = existing.size();
            i--;
          }
        }
      }
    }
    return new Result(movies, existing);
  }

  public static class Result {

    private List<File> filesToSubmit;
    private List<Movie> moviesToRemove;

    public Result(List<File> filesToSubmit, List<Movie> moviesToRemove) {
      this.filesToSubmit = filesToSubmit;
      this.moviesToRemove = moviesToRemove;
    }

    public List<File> getFilesToSubmit() {
      return filesToSubmit;
    }

    public List<Movie> getMoviesToRemove() {
      return moviesToRemove;
    }
  }
}
